//
//  StatusRetweetedView.swift
//  sinaWeibo
//
//  Created by liii on 16/7/29.
//  Copyright © 2016年 liii. All rights reserved.
//

import UIKit
import SnapKit

class StatusRetweetedView: UIView {
    
    // 保存约束
    var bottomConstraint: Constraint?
    // 重写set
    var retweetedStatus: Status? {
        didSet {
            // 绑定数据
//            retweetedLabel.text = retweetedStatus?.text
            retweetedLabel.attributedText = EmoticonManager.sharedEmotionManager.replaceTextToImage(retweetedStatus?.text ?? "", font: retweetedLabel.font)
            
            // 卸载约束
            self.bottomConstraint?.uninstall()
            // 根据配图的有无, 确定高度
            if let urls = retweetedStatus?.imageURLs where urls.count != 0 {
                // 有配图
                retweetedPicView.hidden = false
                // 传递数据
                retweetedPicView.imageURLs = urls
                // 更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(retweetedPicView.snp_bottom).offset(StatusCellMargin).constraint
                })
            } else {
                // 没有配图
                retweetedPicView.hidden = true
                // 清空数据
                retweetedPicView.imageURLs = nil
                // 更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(retweetedLabel.snp_bottom).constraint
                })
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 界面构建
    private func setupUI() {
        
//        backgroundColor = UIColor(white: 0.85, alpha: 1)

        addSubview(retweetedLabel)
        addSubview(retweetedPicView)
    
        retweetedLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.left.equalTo(self).offset(StatusCellMargin)
            make.right.equalTo(self).offset(-StatusCellMargin)
        }
        retweetedPicView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(retweetedLabel.snp_left)
//            make.size.equalTo(CGSizeMake(150, 100))
        }
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstraint = make.bottom.equalTo(retweetedPicView.snp_bottom).offset(StatusCellMargin).constraint
        }
    }

    // 转发微博内容
    private lazy var retweetedLabel: UILabel = UIFactory.labelWithText("转发微博,测试数据!!", fontSize: 16, textColor: UIColor.orangeColor(), alpha: 1, alignment: .Left)
    // 转发微博配图
    private lazy var retweetedPicView: StatusOriginalViewPictureView = StatusOriginalViewPictureView()
    
}
